package fx.util;

import cn.hutool.core.thread.NamedThreadFactory;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 定时器线程池
 * @author sheh
 */
public class StockGetThreadUtil {

    private static volatile ScheduledExecutorService threadPoolExecutor;

    private static final int INITIAL_DELAY = 2;

    private static final int PERIOD = 5;

    private static ScheduledExecutorService getThreadPoolExecutor() {
        if (threadPoolExecutor == null) {
            synchronized (StockGetThreadUtil.class) {
                if (threadPoolExecutor == null) {
                    threadPoolExecutor = new ScheduledThreadPoolExecutor(10, new NamedThreadFactory("stock", true));
                }
            }
        }
        return threadPoolExecutor;
    }

    public static void submitGetDateJob(Runnable runnable){
        getThreadPoolExecutor().scheduleAtFixedRate(runnable, INITIAL_DELAY, PERIOD, TimeUnit.SECONDS);
    }

    public static void close(){
        threadPoolExecutor.shutdown();
    }
}
